#
# MAD Nuke Mod
# CvMADNukeEventManager
# 

from CvPythonExtensions import *

import CvUtil
import CvEventManager
import sys
import PyHelpers
import CvConfigParser
import math
import time
			
gc = CyGlobalContext()	

PyPlayer = PyHelpers.PyPlayer
PyGame = PyHelpers.PyGame()

localText = CyTranslator()

# Change the value to false if spawned pirate ships should not carry any units.
# Default value is true
g_bPirateShipsCanCarryCrew = True

# Increase or decrease the value to change the chance that a spawned pirate 
# ship will carry a crew.
# Default value is 50
g_iPirateShipsCarryCrewChance = 50

def loadConfigurationValues():
	global g_bPirateShipsCanCarryCrew
	global g_iPirateShipsCarryCrewChance
		
	config = CvConfigParser.CvConfigParser("Pirates Mod Config.ini")

	if(config != None):
		g_bPirateShipsCanCarryCrew = config.getboolean("Pirates Mod", "Pirate Ships Can Carry Crew", True)
		g_iPirateShipsCarryCrewChance = config.getint("Pirates Mod", "Pirate Ships Carry Crew Chance", 50)


class CvPiratesModEventManager:
	
	def __init__(self, eventManager):

		eventManager.addEventHandler("unitCreated", self.onUnitCreated)
		eventManager.addEventHandler("windowActivation", self.onWindowActivation)
 		eventManager.addEventHandler("mouseEvent", self.onMouseEvent)
		
		loadConfigurationValues()
	
	def onMouseEvent(self, argsList):
		eventType,mx,my,px,py,interfaceConsumed,screens = argsList

#		gc.getPlayer(18).killUnits()		

		objPlot = gc.getMap().plot(px,py)
	
	def onWindowActivation(self, argsList):
		'Called when the game window activates or deactivates'
		bActive = argsList[0]
		
		if(bActive):
			loadConfigurationValues()	
	
		
	def onUnitCreated(self, argsList):
		'Unit Completed'
		unit = argsList[0]

		# Return immediately if the unit isn't a barbarian unit.
		if(not unit.isBarbarian()):
			return
					
		# Return immediately if the unit spawned isn't a barbarian unit
		if(gc.getPlayer(unit.getOwner()).getCivilizationType() != gc.getInfoTypeForString("CIVILIZATION_BARBARIAN")):
			return

		# Return immediately if the barbarian spawned is not a ship
		if(unit.getDomainType() != gc.getInfoTypeForString("DOMAIN_SEA")):
			return

		# Return immediately if barbarian ships cannot carry crews
		if(not g_bPirateShipsCanCarryCrew):
			return
					
		# Return immediately if barbarian ships cannot carry crews
		if(unit.cargoSpace() == 0):
			return
			
		# Get the list of possible units that can be created for barbarian ships
		unitInfoList = self.getBuildableUnits(unit.getOwner())
			
		# Get the barbarian player
		objPlayer = gc.getPlayer(unit.getOwner())

		##
		## LunarMongoose Fix+Tweaks BEGIN
		##

		iCreationTest = gc.getGame().getSorenRandNum(100, 'Creation Test')
		if (iCreationTest < g_iPirateShipsCarryCrewChance):
			for i in range(unit.cargoSpace()):
				iUnitInfoID = gc.getGame().getSorenRandNum(len(unitInfoList), 'Random Unit Select')
				objNewUnit = objPlayer.initUnit(unitInfoList[iUnitInfoID], unit.plot().getX(), unit.plot().getY(), UnitAITypes.UNITAI_ATTACK)
				objNewUnit.setTransportUnit(unit)
			unit.setUnitAIType(UnitAITypes.UNITAI_ASSAULT_SEA)

		##
		## LunarMongoose Fix+Tweaks END
		##




	# Returns the list of units that can be built by the player ID passed in.				
	def getBuildableUnits(self, iPlayer):
	
		unitInfoList = []

		# Return an empty list if the player passed in is invalid		
		if(iPlayer < 0):
			return []
						
		# Get the player object
		objPlayer = gc.getPlayer(iPlayer)

		# Get the player's civilization info		
		objCivilizationInfo = gc.getCivilizationInfo(objPlayer.getCivilizationType())
		
		# Go through all of the units in the game
		for iUnitInfoID in range (gc.getNumUnitInfos()):

			# Continue if the player cannot train the current unit
			if(not objPlayer.canTrain(iUnitInfoID, false, false)):
				continue
				
			# Continue if the civilization cannot train the current unit
			if(objCivilizationInfo.getCivilizationUnits(iUnitInfoID) == UnitTypes.NO_UNIT):
				continue
				
			# Get the unit info object
			objUnitInfo = gc.getUnitInfo(iUnitInfoID)
					
			# Continue if the current unit info is an animal
			if(objUnitInfo.isAnimal()):
				continue
					
			# Continue if the current unit info is not a land unit
			if(objUnitInfo.getDomainType() != gc.getInfoTypeForString("DOMAIN_LAND")):
				continue
				
			# Continue if the current unit info does not have an attack UNITAI
			if(not objUnitInfo.getUnitAIType(gc.getInfoTypeForString("UNITAI_ATTACK"))):
				continue
					
			# Append the unit info ID to the list
			unitInfoList.append(iUnitInfoID)

				
		return unitInfoList
		
		
		